package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.controller.SearchController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.client.panel.SearchCrowdPanel;
import cn.hnu.qq.client.panel.SearchFriendsPanel;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.entity.message.ResponseSearchMessage;
import cn.hnu.qq.global.MessageType;

public class SearchDialog extends JDialog implements ActionListener,ChangeListener,MessageListener{

	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};

	private SearchController controller = SearchController.getInstance();
	
	private JTabbedPane tabbedPane;
	
	private JButton okButton;
	
	private SearchFriendsPanel sfp;
	
	private SearchCrowdPanel scp;
	
	private boolean sfpSearched = false;
	
	private boolean scpSearched = false;
	
//	private SearchController controller = SearchController.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SearchDialog dialog = new SearchDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SearchDialog() {
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	public SearchDialog(Frame owner, boolean modal) {
		super(owner, modal);
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void init() throws IOException {
		setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg"))); 
		setTitle("查找对话框");
		setBounds(250, 100, 375, 345);
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.addChangeListener(this);
			contentPanel.add(tabbedPane, BorderLayout.CENTER);
			JPanel p = new JPanel();
			tabbedPane.add(p,0);
			p.setLayout(new BorderLayout());
			sfp = new SearchFriendsPanel();
			p.add(sfp,BorderLayout.CENTER);
			tabbedPane.setTitleAt(0, "查找好友");
			scp = new SearchCrowdPanel();
			tabbedPane.add(scp,1);
			tabbedPane.setTitleAt(1, "查找群组");
			tabbedPane.setSelectedIndex(0);
			tabbedPane.setPreferredSize(new Dimension(sfp.getWidth()+10, sfp.getHeight()));
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.EAST);
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.SOUTH);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("添加好友");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener(this);
			}
		}
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("OK")){
			int index = tabbedPane.getSelectedIndex();
			if(index == 0){
				if(sfp.getSearchedAccount() == null || sfp.getSearchedAccount().equals("")){
					JOptionPane.showMessageDialog(this, "尚未查找！！");
					return;
				}
				//添加好友请求
				controller.addFriend(sfp.getSearchedAccount());
				JOptionPane.showMessageDialog(this, "请等待对方接受");
				return;
			}else{
				if(scp.getSearchedName() == null || scp.getSearchedName().equals("")){
					JOptionPane.showMessageDialog(this, "尚未查找！！");
					return;
				}
				controller.joinCrowd(scp.getSearchedName());
				JOptionPane.showMessageDialog(this, "请等待群组接受");
			}
		}
		if(e.getActionCommand().equals("Cancel")){
			this.dispose();
		}
		
	}

	public void stateChanged(ChangeEvent e) {
		if(e.getSource() == tabbedPane && okButton != null){
			int index = tabbedPane.getSelectedIndex();
			if(index == 0){
				okButton.setText("添加好友");
				tabbedPane.setPreferredSize(new Dimension(1000,2000));
			}else if(index == 1){
				okButton.setText("加入群组");
			}
		}
		
	}

	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		if(message.getMessageType() == MessageType.SEARCH_USER){
			ResponseSearchMessage rsm = (ResponseSearchMessage) message;
			User user = (User) rsm.getScource();
			if(user == null){
				JOptionPane.showMessageDialog(this, "用户不存在");
				return ;
			}
			return;
		}
		if(message.getMessageType() == MessageType.SEARCH_CROWD){
			ResponseSearchMessage rsm = (ResponseSearchMessage) message;
			Crowd crowd = (Crowd) rsm.getScource();
			if(crowd == null){
				JOptionPane.showMessageDialog(this, "群组不存在");
				return;
			}
			return;
		}
		if(message.getMessageType() == MessageType.CONFIRM_ADD_FRIEND){
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					JOptionPane.showMessageDialog(SearchDialog.this, "添加好友成功");
				}
			});
			return;
		}
		if(message.getMessageType() == MessageType.FRIEND_EXIT){
			JOptionPane.showMessageDialog(this, "对方已经是你的好友，无需添加");
			return;
		}
		if(message.getMessageType() == MessageType.ALREADY_IN_CROWD){
			JOptionPane.showMessageDialog(this, "你已经在该群组中，无需加入");
			return;
		}
		if(message.getMessageType() == MessageType.CONFIRM_JOIN_CROWD){
			JOptionPane.showMessageDialog(this, "加入群组成功");
			return;
		}
	}

}
